import 'package:duqin/config/app_colors.dart';
import 'package:duqin/util/user_type.dart';
import 'package:flutter/material.dart';

class AvatarRoleName extends StatelessWidget {
  final String avatar;
  final String nickname;
  final String type;
  final bool showType;

  const AvatarRoleName(
      {Key? key,
      required this.avatar,
      required this.nickname,
      required this.type,
      this.showType = true})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        _headImg(),
        Offstage(
          //隐藏与显示
          offstage: !showType,
          child: _role(),
        ),
        _nickName(),
      ],
    );
  }

  _headImg() {
    return Container(
      width: 22,
      height: 22,
      child: ClipOval(
        child: FadeInImage.assetNetwork(
          placeholder: 'assets/images/common/lazy-1.png',
          image: avatar,
          fit: BoxFit.cover,
        ),
      ),
    );
  }

  _nickName() {
    return Expanded(
        child: Container(
      margin: EdgeInsets.only(left: 6),
      child: Text(
        nickname,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(fontSize: 14, color: AppColors.unactive),
      ),
    ));
  }

  Widget _role() {
    return Container(
      margin: EdgeInsets.only(left: 6),
      padding: EdgeInsets.symmetric(vertical: 2, horizontal: 4),
      decoration: BoxDecoration(
        color: UserType.formColor(type),
        borderRadius: BorderRadius.circular(4),
      ),
      child: Text(
        UserType.formCn(type),
        style: TextStyle(color: Colors.white, fontSize: 10),
      ),
    );
  }
}
